<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ page import="com.smarthome.web.property.utils.PermissionUtil"  %>

<%
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<jsp:include page="${path}/views/layout/inc.jsp"></jsp:include>
<script type="text/javascript">
	$.canEdit = true;
	$.canDelete = true;
	var typeGrid;
	$(function(){
		typeGrid=$("#typeGrid").datagrid({
			url:"/deviceType/dataGrid",
			width:'100%',
			nowrap: false,
			striped: true,
			fit:true,
			collapsible:true,
			pageNumber:1,
			pageSize:10,
			pageList:[10],
			fitColumns:true,
			columns:[[
				{field:'name',title:'设备类型名称',width:100},
				{field:'equipmentTypeCode',title:'类型唯一标识',width:80},
				{field:'gmtCreate',title:'新增时间',width:100,sortable:true,formatter : function(value, row, index) {
					var unixTimestamp = new Date(value);  
                    return unixTimestamp.format("yyyy-MM-dd hh:mm:ss");
				}},
				{field:'remark',title:'备注',width:100},
				{field:'isDel',title:'启用状态',width:100,formatter : function(value, row, index) {
					var ok = "<span style='color:green'>启用</span>";
					var not = "<span style='color:red'>禁用</span>";
					return value==0?ok:not;
				}},
				{field:'action',title:'操作',width:100,formatter : function(value, row, index) {
					var str = '';
					if ($.canEdit) {
						str += $.formatString('<img onclick="editDeviceType(\'{0}\');" src="{1}" title="编辑"/>', index, '${path}/images/extjs_icons/pencil.png');
					}
					str += '&nbsp;';
					if ($.canDelete) {
						str += $.formatString('<img onclick="delDeviceType(\'{0}\');" src="{1}" title="删除"/>', index, '${path}/images/extjs_icons/cancel.png');
					}
					return str;
				}}
			]],
			sortName: 'gmtCreate',
			sortOrder: 'desc',
			checkOnSelect : false,
			selectOnCheck : false,
			pagination:true,
			rownumbers:true,
			singleSelect:true, // 是否单选
			toolbar:"#toolbar",
			onLoadSuccess : function() {
				parent.$.messager.progress('close');
// 				$(this).treegrid('tooltip');
			},
			onRowContextMenu : function(e, index, row){
				e.preventDefault();
				typeGrid.datagrid("unselectAll");
				typeGrid.datagrid("selectRow",index);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});
	});
	function addDeviceType(){
		parent.$.modalDialog({
			title : '新增设备类型',
			width : 500,
			height : 200,
			href : '/deviceType/addPage',
			buttons : [ {
				text : '确认新增',
				handler : function() {
					parent.$.modalDialog.openner_typeGrid = typeGrid;//因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}
	function editDeviceType(index){
		if(index)typeGrid.datagrid("selectRow",index);
		var row = typeGrid.datagrid('getSelected');
		if (row) {
			parent.$.modalDialog({
				title : '修改设备类型',
				width : 500,
				height : 200,
				href : '/deviceType/editPage?id=' + row.id,
				buttons : [ {
					text : '确认修改',
					handler : function() {
						parent.$.modalDialog.openner_typeGrid = typeGrid;//因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
						var f = parent.$.modalDialog.handler.find('#form');
						f.submit();
					}
				} ]
			});
		}
	}
	function delDeviceType(index){
		if(index)typeGrid.datagrid("selectRow",index);
		var row = typeGrid.datagrid('getSelected');
		if (row) {
			if(row.isDel==1){
				parent.$.messager.alert('提示', "该设备类型已删除", 'info');return;
			}
			parent.$.messager.confirm('询问', '您是否要删除该设备类型？', function(b) {
				if (b) {
					parent.$.messager.progress({
						title : '提示',
						text : '数据处理中，请稍后....'
					});
					$.post('/deviceType/delete', {
						id : row.id
					}, function(result) {
						if (result.success) {
							parent.$.messager.alert('提示', result.msg, 'info');
							typeGrid.datagrid('reload');
						}
						parent.$.messager.progress('close');
					}, 'JSON');
				}
			});
		}
	}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>设备类型</title>
</head>
<body>
	<div class="easyui-layout" data-options="fit:true,border:false">
			<div data-options="region:'center',border:false" title="" style="overflow: hidden;">
				<table id="typeGrid"> </table>
			</div>
			<div id="toolbar" style="display: none;">
					<a onclick="addDeviceType();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">添加</a>
			</div>
	</div>
	<div id="menu" class="easyui-menu" style="width: 120px; display: none;">
			<div onclick="addDeviceType();" data-options="iconCls:'pencil_add'">增加</div>
			<div onclick="editDeviceType();" data-options="iconCls:'pencil_delete'">编辑</div>
			<div onclick="delDeviceType();" data-options="iconCls:'pencil'">删除</div>
	</div>
</body>
</html>